package database;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import model.User;
import dao.*;
public class UserDaoImpl extends DatabaseConnector implements UserDao {

	public UserDaoImpl() throws ClassNotFoundException, IllegalAccessException,
			InstantiationException {
		super();
	}

	public boolean validateUser(User user){
		// return true if the user is valid
		String sql = "select * from userInfo where user_id='"+user.userId+"'";
		boolean b= false;
		try{
			connectDB();
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()){
				String password= rs.getString("user_password");
				if(password.equals(user.userPassword)){
					b = true;
				}
			}
			rs.close();		
			closeDB();
		}catch(SQLException sqle){
			return false;
		}
		return b;
	}
	public boolean containsUser(User user){
		// return true if the user is valid
		String sql = "select * from userInfo where user_id='"+user.userId+"'";
		boolean b= false;
		try{
			connectDB();
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()){
				b = true;
			}
			rs.close();		
			closeDB();
		}catch(SQLException sqle){
			return false;
		}
		return b;
	}
	public boolean insertUser(User user){
		String sql = "insert into userInfo values(?,?,?,?,?,?,?,?)";
		try{
			connectDB();
			PreparedStatement ps = connection.prepareStatement(sql); 
			ps.setString(1, user.userId);
			ps.setString(2, user.userPassword);
			ps.setString(3, user.userType);
			ps.setString(4, user.userName);
			ps.setString(5, user.userGender);
			ps.setString(6, user.userBirthday);
			ps.setString(7, user.userEmail);
			ps.setString(8, user.userCellPhone);
			ps.execute();
			closeDB();
			return true;
		}catch (SQLException sqle){
			return false;
		}
	}
	public boolean deleteUserByUserId(String userId){
		String sql = "delete from userInfo where user_id ='"+userId+"'";
		try{
			connectDB();
			statement.execute(sql);
			closeDB();
			return true;
		}catch(SQLException sqle){
			return false;
		}
	}
	public boolean updateUser(User user){
		String sql = "update userInfo set user_type =?, user_name=?,user_gender=?," +
				"user_birthday=?,user_email=?,user_cellphone =? where user_id =?";
		try{
			connectDB();
			PreparedStatement ps = connection.prepareStatement(sql);
			ps.setString(1, user.userType);
			ps.setString(2, user.userName);
			ps.setString(3, user.userGender);
			ps.setString(4, user.userBirthday);
			ps.setString(5, user.userEmail);
			ps.setString(6, user.userCellPhone);
			ps.setString(7, user.userId);
			ps.executeUpdate();
			closeDB();
			return true;
		}catch(SQLException sqle){
			return false;
		}
	}
	public User getUserByUserId(String userId){
		String sql = "select * from userInfo where user_id='"+userId+"'";
		User user = new User();
		user.userId = userId;
		try{
			connectDB();
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()){
				user.userPassword = rs.getString("user_password");
				user.userType = rs.getString("user_type");
				user.userName = rs.getString("user_name");
				user.userGender = rs.getString("user_gender");
				user.userBirthday = rs.getString("user_birthday");
				user.userEmail = rs.getString("user_email");
				user.userCellPhone = rs.getString("user_cellphone");
			}
			rs.close();
			closeDB();
		}catch(SQLException sqle){
			
		}
		return user;
	}
	public boolean isContainUserId(String userId){
		User user = getUserByUserId(userId);
		if(user.userPassword!=null){
			return true;
		}
		return false;
		
	}
}
